package com.wanxi.service.user.impl;

import com.wanxi.dao.user.IUserDao;
import com.wanxi.domain.ResultModel;
import com.wanxi.domain.user.UserModel;
import com.wanxi.domain.user.vo.UserModelVo;
import com.wanxi.domain.user.vo.UserPageVo;
import com.wanxi.service.user.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private IUserDao userDao;


    @Override
//    public ResultModel checkLogin(UserModel userModel) {
    public UserModel checkLogin(UserModel userModel) {
        //跟据用户名和密码得到用户信息
        UserModel userInfo = userDao.selectByNameAndPassword(userModel.getUsername(), userModel.getPassword());

       /* ResultModel resultModel = new ResultModel();

        if (userInfo.getUsername() == null) {
            resultModel.setMsg("error");
            return resultModel;
        } else if (!userModel.getCode().equals(userModel.getSessionCode())) {
            resultModel.setMsg("code-error");
            return resultModel;
        } else {
            resultModel.setMsg("success");
            resultModel.setData(userInfo);
            return resultModel;
        }*/

//       return resultModel;
       return userInfo;
    }

    @Override
    public  List<UserModel> selectAll(int curr, int limit) {
        List<UserModel> userModels = userDao.selectAll(curr, limit);

        /*ResultModel resultModel = new ResultModel();
        if (userModels.size() == 0) {
            resultModel.setMsg("error");
            resultModel.setData(userModels);
            return resultModel;
        }
        resultModel.setMsg("success");
        resultModel.setData(userModels);*/
        return userModels;
    }

    @Override
    public int insertUser(UserModel userModel) {
        int num = userDao.insertUser(userModel);

       /* ResultModel resultModel = new ResultModel();
        if (num > 0) {
            resultModel.setMsg("success");
            resultModel.setCount(num);
        } else {
            resultModel.setMsg("error");
            resultModel.setCount(0);
        }*/
        return num;
    }

    @Override
    public UserModel selectById(Integer id) {

        UserModel user = userDao.selectById(id);
        /*ResultModel resultModel = new ResultModel();

        if (user.getUsername() != null) {
            resultModel.setMsg("success");
            resultModel.setData(user);
        } else {
            resultModel.setMsg("error");
        }*/

        return user;
    }

    @Override
    public int update(UserModel userModel) {
        int num = userDao.update(userModel);

        /*ResultModel resultModel = new ResultModel();
        if (num > 0) {
            resultModel.setMsg("success");
            resultModel.setCount(num);
        } else {
            resultModel.setMsg("error");
            resultModel.setCount(0);
        }*/
        return num;
    }

    @Override
    public int deleteById(UserModel userModel) {
        int num = userDao.deleteById(userModel);

        ResultModel resultModel = new ResultModel();
        if (num > 0) {
            resultModel.setMsg("success");
            resultModel.setCount(num);
        } else {
            resultModel.setMsg("error");
            resultModel.setCount(0);
        }
        return num;
    }

    @Override
    public int getCount() throws FileNotFoundException {

        /*手动制造异常*/
        File file = new File("aaa");
        InputStream inputStream = new FileInputStream(file);

        int count = userDao.getCount();
        /*ResultModel resultModel = new ResultModel();
        if(count==0){
            resultModel.setMsg("error");
            return resultModel;
        }
        resultModel.setMsg("success");
        resultModel.setData(count);*/
        return count;
    }

    //模糊查询
    @Override
    public List<UserModel> globalSearch(UserPageVo userPageVo) {
        List<UserModel> user = userDao.globalSearch(userPageVo);

       /* ResultModel resultModel = new ResultModel();
        if (user != null) {
            resultModel.setMsg("success");
            resultModel.setData(user);
        } else {
            resultModel.setMsg("error");
        }*/

        return user;
    }

    //模糊查询条数
    @Override
    public int getGlobalSeachCount(UserModelVo userModelVo) {
        int count = userDao.getGlobalSeachCount(userModelVo);

       /* ResultModel resultModel = new ResultModel();
        if(count<=0){
            resultModel.setMsg("error");
        }

        resultModel.setMsg("success");
        resultModel.setCount(count);*/

        return count;
    }
}
